<?php
/**
 * RestAPIController.php
 *
 * Contains all of the supporting classes and methods needed for the dashboard api
 * @package PHPZendOpcacheBundle
 * @author Steven King <kingrst@gmail.com>
 * @copyright Copyright (C) 2014, Steven King
 * @license http://opensource.org/licenses/GPL-3.0 GNU General Public License, version 3
 */

namespace PHP\ZendOpcacheBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Yaml\Yaml;
use LSS\Array2XML;

use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\View\RouteRedirectView;
use FOS\RestBundle\View\View;
use FOS\RestBundle\Controller\Annotations\QueryParam;
use FOS\RestBundle\Request\ParamFetcherInterface;

use Nelmio\ApiDocBundle\Annotation\ApiDoc;

/**
 * RestAPIController Class for PHPZendOpcacheBundle
 *
 * @package PHPZendOpcacheBundle\Controller
 * @subpackage RestAPIController
 * @category class
 * @author Steven King <kingrst@gmail.com>
 */
class RestAPIController extends Controller {
  /**
   * {@inheritdoc}
   */

  static protected $api_version = '1.0';

  private static function &xmlFormatArray($array) {
    foreach ($array as $n => $v) {
      if (is_array($v)) {
        if (preg_match('/^\/.*/',$n)) {
          $v['@attributes'] = array('full_path' => $v['full_path']);
	  unset($array[$v['full_path']]);
	  unset($v['full_path']);

	  $array['file'][] = self::xmlFormatArray($v);
        } else {
	  $array[$n] = self::xmlFormatArray($v);
	}
      } else {
        $array[$n] = htmlspecialchars($v, ENT_XML1, 'UTF-8');
      }
    }

    return $array;
  }

  /**
   * @ApiDoc(
   * description="Get status information from Opcache module.",
   * parameters={
   *  {"name"="_format", "dataType"="string", "required"="true", "description"="Return status in format specified."}
   * },
   * requirements={
   *  {"name"="_format", "dataType"="string", "requirement"="json|yaml|serialized|xml", "description"="Return stats in specified format."}
   * }
   * )
   */
  public function getStatsAction() {
    $opcache_status_array = opcache_get_status();

    if (!isset($_format)) {
      $_format = 'json';
      $format = $_format;
    }

    if ($format == 'json') {
      $content_type = 'application/json';
      $status_code = 200;
      $opcache_status = json_encode($opcache_status_array);
    } elseif ($format == 'yaml') {
      $content_type = 'applicaton/yaml';
      $status_code = 200;
      $opcache_status = Yaml::dump($opcache_status_array);
    } elseif ($format == 'serialized') {
      $content_type = 'text/plain';
      $status_code = 200;
      $opcache_status = serialize($opcache_status_array);
    } elseif ($format == 'xml') {
      $content_type = 'application/xml';

      $opcache_status_xml_array = $this->xmlFormatArray($opcache_status_array);
      $xml = Array2XML::createXML('opcache_status', $opcache_status_xml_array);

      $status_code = 200;
      $opcache_status = $xml->saveXML();
    } else {
      $content_type = 'text/plain';
      $status_code = 500;
      $opcache_status = "ERROR:Invalid format";
    }

    $headers = array( $content_type );

    $view = new View($opcache_status, $status_code, $headers);
    $view->setTemplate('PHPZendOpcacheBundle:Rest:getStats.html.twig');

    return $this->get('fos_rest.view_handler')->handle($view);
  } // "get_status" [GET] /api/get_status.{_format}
}
